package com.lxc.demos.android.handler;

import com.lxc.demos.R;
import com.lxc.demos.util.Log;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MultithreadActivity extends Activity {  
	  
    private TextView txView1,txView2,txView3,txView4;  
    private Button button;  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        Log.i("RootyInfo", "oncreate");  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.multithread);      
         
        txView1=(TextView)findViewById(R.id.textView1);  
        txView2=(TextView)findViewById(R.id.textView2);  
        txView3=(TextView)findViewById(R.id.textView3);  
        txView4=(TextView)findViewById(R.id.textView4);  
        button=(Button)findViewById(R.id.button1);  
        button.setOnClickListener(new OnClickListener() {  
            
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                //创建一个用于展示AsyncTask实现交互的TestAsyncTask  
                new TestAsyncTask().execute("Test"," AsyncTask");  
           
                SystemClock.sleep(1000);
                
         
                //创建一个用于展示前三种后台线程和UI线程交互的线程  
                new TestThread(MultithreadActivity.this).start();  
            }  
        });  
    }  
    
    
    class TestAsyncTask extends AsyncTask<String, Integer, String>  
    {

    	//TestAsyncTask被后台线程执行后，被UI线程被调用，一般用于初始化界面控件，如进度条  
        @Override  
        protected void onPreExecute() {  
            // TODO Auto-generated method stub  
            super.onPreExecute();  
        }  

        //doInBackground执行完后由UI线程调用，用于更新界面操作  
        @Override  
        protected void onPostExecute(String result) {  
            // TODO Auto-generated method stub  
            txView1.setText(result);  
            super.onPostExecute(result);  
        }  

        //在PreExcute执行后被启动AysncTask的后台线程调用，将结果返回给UI线程  
        @Override  
        protected String doInBackground(String... params) {  
            // TODO Auto-generated method stub  
            StringBuffer sb=new StringBuffer();  
            for (String string : params) {  
                sb.append(string);  
            }  
            return sb.toString();  
        }  
        

    }  

    
    class TestHandler extends Handler  {
        public TestHandler(Looper looper) {  
            super(looper);  
        }  
        
        @Override  
        public void handleMessage(Message msg) {  
            // TODO Auto-generated method stub  
            System.out.println("123");  
            txView2.setText((String)msg.getData().get("tag"));  
        }  
          
    }
    
    class TestThread extends Thread{
    	  Activity activity;
    	  public TestThread(Activity activity){
    		  this.activity = activity;
    	  }
		@Override
		public void run() {
			
			//下面代码用来演示Activity.runOnUIThread(Runnable)方法的实现  
			activity.runOnUiThread(new Runnable(){

				@Override
				public void run() {
					txView3.setText("Test runOnUIThread");
					
				}
				
			});
			

			SystemClock.sleep(1000);
			
			txView4.postDelayed(new Runnable(){
				@Override
				public void run() {
					// TODO Auto-generated method stub
					txView4.setText("test View.post(Runnable)");
					
				}
			}, 1000);
			
			
			SystemClock.sleep(1000);
			Message msg = new Message();
			Bundle bundle = new Bundle();
			bundle.putString("tag", "Test Handler");
			msg.setData(bundle);
			new TestHandler(Looper.getMainLooper()).sendMessage(msg);
			
			
		}
		
		
	
    	  
		
    	  
    }

   
}